<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统设置 - Bou Scheduler</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">Bou Scheduler</a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/tasks">任务管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/monitor">监控统计</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/settings">系统设置</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <div class="container mt-4">
        <h1>系统设置</h1>
        
        <!-- 调度器控制 -->
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>调度器控制</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <span>当前状态:</span>
                            <span th:if="${isRunning}" class="badge bg-success">运行中</span>
                            <span th:unless="${isRunning}" class="badge bg-danger">已停止</span>
                        </div>
                        
                        <div class="d-grid gap-2">
                            <button th:if="${!isRunning}" class="btn btn-success" onclick="startScheduler()">
                                启动调度器
                            </button>
                            <button th:if="${isRunning}" class="btn btn-danger" onclick="stopScheduler()">
                                停止调度器
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>快速操作</h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <button class="btn btn-outline-primary" onclick="cleanupTasks()">
                                清理已完成任务
                            </button>
                            <button class="btn btn-outline-warning" onclick="resetStatistics()">
                                重置统计信息
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 系统信息 -->
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>系统信息</h5>
                    </div>
                    <div class="card-body">
                        <p><strong>应用名称:</strong> Bou Scheduler</p>
                        <p><strong>版本:</strong> 1.0.0</p>
                        <p><strong>Java版本:</strong> JDK 24</p>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>存储信息</h5>
                    </div>
                    <div class="card-body">
                        <p><strong>存储类型:</strong> Redis</p>
                        <p><strong>连接状态:</strong> <span class="badge bg-success">正常</span></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/js/utils.js"></script>
    <script>
        // 启动调度器
        function startScheduler() {
            const btn = event.target;
            const originalText = btn.innerHTML;
            
            btn.disabled = true;
            btn.innerHTML = '<i class="bi bi-hourglass-split"></i> 启动中...';
            
            fetch('/api/tasks/scheduler/start', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showSuccess('调度器启动成功！');
                    setTimeout(() => location.reload(), 1000);
                } else {
                    showError('启动失败: ' + data.message);
                    btn.disabled = false;
                    btn.innerHTML = originalText;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showError('启动失败，请检查网络连接');
                btn.disabled = false;
                btn.innerHTML = originalText;
            });
        }

        // 停止调度器
        function stopScheduler() {
            if (!confirm('确定要停止调度器吗？这将停止所有任务的执行。')) {
                return;
            }

            const btn = event.target;
            const originalText = btn.innerHTML;
            
            btn.disabled = true;
            btn.innerHTML = '<i class="bi bi-hourglass-split"></i> 停止中...';

            fetch('/api/tasks/scheduler/stop', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showSuccess('调度器停止成功！');
                    setTimeout(() => location.reload(), 1000);
                } else {
                    showError('停止失败: ' + data.message);
                    btn.disabled = false;
                    btn.innerHTML = originalText;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showError('停止失败，请检查网络连接');
                btn.disabled = false;
                btn.innerHTML = originalText;
            });
        }

        // 清理已完成任务
        function cleanupTasks() {
            if (!confirm('确定要清理已完成的任务吗？这将删除所有已成功、失败或已取消的任务记录。')) {
                return;
            }

            const btn = event.target;
            const originalText = btn.innerHTML;
            
            btn.disabled = true;
            btn.innerHTML = '<i class="bi bi-hourglass-split"></i> 清理中...';

            // 设置清理时间为1小时前
            const beforeTime = new Date(Date.now() - 60 * 60 * 1000).toISOString();
            
            fetch(`/api/tasks/cleanup?beforeTime=${beforeTime}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showSuccess(`清理完成！共清理 ${data.data} 个已完成任务`);
                    setTimeout(() => location.reload(), 1000);
                } else {
                    showError('清理失败: ' + data.message);
                    btn.disabled = false;
                    btn.innerHTML = originalText;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showError('清理失败，请检查网络连接');
                btn.disabled = false;
                btn.innerHTML = originalText;
            });
        }

        // 重置统计信息
        function resetStatistics() {
            if (!confirm('确定要重置统计信息吗？这将清除所有性能指标和任务计数，此操作不可逆。')) {
                return;
            }

            const btn = event.target;
            const originalText = btn.innerHTML;
            
            btn.disabled = true;
            btn.innerHTML = '<i class="bi bi-hourglass-split"></i> 重置中...';

            fetch('/api/tasks/scheduler/statistics/reset', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showSuccess('统计信息重置成功！');
                    setTimeout(() => location.reload(), 1000);
                } else {
                    showError('重置失败: ' + data.message);
                    btn.disabled = false;
                    btn.innerHTML = originalText;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showError('重置失败，请检查网络连接');
                btn.disabled = false;
                btn.innerHTML = originalText;
            });
        }
    </script>
</body>
</html>
